<?php

namespace iBrand\Shop\Backend\Http\Controllers;

use Encore\Admin\Facades\Admin as LaravelAdmin;
use Encore\Admin\Layout\Content;
use iBrand\Component\Goods\Models\GoodsProduct;
use iBrand\Component\Goods\Models\SpecsValue;
use iBrand\Component\Goods\Repositories\GoodsRepository;
use iBrand\Component\Purchase\Repositories\StockRecordRepository;
use iBrand\Shop\Backend\Service\CommonService;
use Illuminate\Http\Request;

class StockRecordController extends Controller
{
    protected $goodsRepository;
    protected $stockRecordRepository;
    protected $commonService;

    public function __construct(GoodsRepository $goodsRepository,
                                StockRecordRepository $stockRecordRepository,
                                CommonService $commonService)
    {
        $this->goodsRepository = $goodsRepository;
        $this->stockRecordRepository = $stockRecordRepository;
        $this->commonService = $commonService;
    }

    public function index()
    {
        $specList = $this->commonService->getSpecList();
        $colors = $specList['colors'];
        $sizes = $specList['sizes'];
        $types = [
            ['id' => 1, 'name' => '进库'],
            ['id' => 2, 'name' => '出库']
        ];

        return LaravelAdmin::content(function (Content $content) use ($colors, $sizes, $types) {

            $content->header('库存流水');

            $content->breadcrumb(
                ['text' => '库存流水', 'url' => '', 'no-pjax' => 1, 'left-menu-active' => '库存流水']
            );

            $content->body(view('shop-backend::stockRecord.index', compact('sizes', 'colors', 'types')));
        });
    }

    public function goodsStoredRecordList(Request $request)
    {
        $params = $request->except('file', '_token');

        $_where = [];
        $where['shop_id'] = session('shop_id');
        if ($params['updateTimeBegin']) {
            $begin = date('Y-m-d', strtotime($params['updateTimeBegin'])) . ' 00:00:00';
            $where['created_at'] = ['>=', $begin];
        }

        if ($params['updateTimeEnd']) {
            $end = date('Y-m-d', strtotime($params['updateTimeEnd'])) . ' 23:59:59';
            $_where['created_at'] = ['<=', $end];
        }

        $item_ids = [];
        if (isset($params['goodsSn']) AND $params['goodsSn']) {
            $goods = $this->goodsRepository->getGoodsByGoodsNo($params['goodsSn']);
            if ($goods) {
                $where['goods_id'] = $goods->id;
            }
        }

        if (isset($params['recordType']) AND $type = $params['recordType']) {
            if ($type == 1) {
                $where['change_count'] = ['>', 0];
            } elseif ($type == 2) {
                $where['change_count'] = ['<', 0];
            }
        }

        $tmpGoodsProduct = GoodsProduct::where('is_show', 1)->first();
        $tmpSpec = SpecsValue::find($tmpGoodsProduct->specID[0]);
        if ($tmpSpec->spec_id == 1) {
            $left_type = 'size';
        } elseif ($tmpSpec->spec_id == 2) {
            $left_type = 'color';
        }

        if ($params['colorId'] && !$params['sizeId']) {
            $like = $left_type == 'color' ? '["' . request('colorId') . '"%' : '%"' . request('colorId') . '"]';
            $products = GoodsProduct::where('is_show', 1)->where('specID', 'like', $like)->get();
            if ($products && $products->count() > 0) {
                $item_ids = $products->pluck('id')->toArray();
            }
        } elseif (!$params['colorId'] && $params['sizeId']) {
            $like = $left_type == 'size' ? '["' . request('sizeId') . '"%' : '%"' . request('sizeId') . '"]';
            $products = GoodsProduct::where('is_show', 1)->where('specID', 'like', $like)->get();
            if ($products && $products->count() > 0) {
                $item_ids = $products->pluck('id')->toArray();
            }
        } elseif ($params['colorId'] && $params['sizeId']) {
            $like = $left_type == 'color' ? '["' . request('colorId') . '","' . request('sizeId') . '"]' : '["' . request('sizeId') . '","' . request('colorId') . '"]';
            $products = GoodsProduct::where('is_show', 1)->where('specID', $like)->get();
            if ($products && $products->count() > 0) {
                $item_ids = $products->pluck('id')->toArray();
            }
        }

        if (!empty($item_ids)) {
            $item_ids = array_unique($item_ids);
        }

        $with = [];
        $record = $this->stockRecordRepository->getStockRecordPaginate($where, $_where, $with, $item_ids, 0);

        return $this->ajaxJson(true, $record->toArray());
    }
}